
//Dom结构

var mask=$('.mask'),//遮罩层
    rainAlert=$('.rainRuleAlert'),
    close=$('.close1'),
    appointSuccess=$('.appointSuccess'),
    rainRule=$('.rainRule');
// 在微信先显示头部的tabs
if(is_WX){
	$('.tabs').show();
	$('.container').addClass('paddTop');
}else{
	$('.tabs').hide();
	$('.container').removeClass('paddTop');
}
// tabs切换
$('.showTabs').on('click',function(){
	if($(this).hasClass('active')){
     $('.showTabs').removeClass('active');
     $('.Items').hide();
	}else{
	 $('.showTabs').addClass('active');
	 $('.Items').show();
	}

})
//红包雨 预约提醒
$('.noappoint').on('click',function(){
	if(!commonSignIn()) return;
	if($(this).hasClass('appoint')){  //已经预约

	}else{
		login=false;
		alertVerification();
		$('.textHead').text('预约提醒');
		$('.toLogin').text('预约提醒');
	}

});

// 摇一摇
$('.device').on('click',function(){
	// if(!commonSignIn()) return;
	// if(_Global_==='app'){
	// 	action(20171102);
	// }else{
	// 	location.href='./shake_index.html'  //跳转到摇一摇页面
	// }
	$('.mask').show();
    $('.promptBox').show();
    $('.promptBox .firstary').html('活动已结束!');
})
//关闭
$('.iKnow').click(function(){
    $('.mask').hide();
    $('.promptBox').hide();
})
//中奖规则弹窗
rainRule.on('click',function(){
	rainAlert.show();
	mask.show();
})
$('.close1,.mask,.knowSucc').on('click',function(){
	rainAlert.hide();
	mask.hide();
    $('.promptBox').hide();
	appointSuccess.hide();
	$('.verification').remove();
	$('.rainStart').remove();
	$('body').css({'overflow':'auto','position':'static'});
})
init();


/*秒杀商品列表*/
function seckillInit(){
	$.ajax({
	    url : getIp + '/CARNT/SeckillServDetail/seckDetailList',
	    type : "post",
	    dataType : "json",
	    data : {
	    },
	    success : function(data) {
	    	if ( data.STATUS_CODE == 1 ) {// 正常返回
	    		setInterval(function(){
	    			var start=data.STARTDATE;
	    			var end=data.ENDDATE;
	    			downTime(start,end);
	    		},500)
	    		for(var i=0;i<data.RESULT_DATA.length;i++){
	    			var widths=1.37*Number(data.RESULT_DATA[i].SALE_NUM/data.RESULT_DATA[i].TOTAL_NUM);
	    			var shopList='<div class="seckillItem" onclick="H5AppOpen('+data.RESULT_DATA[i].SERV_ID+')">'+
	    							'<img src="'+data.IMG_URL+data.RESULT_DATA[i].COVER_IMG+'" />'+
	    							'<div class="progressMess"><div class="progress">'+
	    							'<div class="progressBar" style="width:'+widths+'rem"></div></div> <span>'+data.RESULT_DATA[i].PROBABILITY+'</span></div>'+
	    							'<div class="prices"><span>￥'+data.RESULT_DATA[i].PRICE+'</span><label>原价:￥<s>'+data.RESULT_DATA[i].ORIGINAL_PRICE+'</s></label></div>'+
	    			            '</div>';
	    			$('.seckillMain').append(shopList);
	    		}
	    	}
	    }
	});
}
function downTime(start,end){
	var newTime=new Date().getTime();
	var objTime={
		flag:true,//代表预告
		H:0,
		M:0,
		S:0
	}
	if(newTime<start){
		var time=start-newTime;
		objTime.flag=true;
		objTime.H=parseInt(time/(60*60*1000));
		objTime.M=parseInt((time-objTime.H*60*60*1000)/(60*1000));
		objTime.S=parseInt((time-objTime.H*60*60*1000-objTime.M*60*1000)/(1000));
	}else if(newTime>=start&&newTime<=end){
		var time=end-newTime;
		objTime.flag=false;
		objTime.H=parseInt(time/(60*60*1000));
		objTime.M=parseInt((time-objTime.H*60*60*1000)/(60*1000));
		objTime.S=parseInt((time-objTime.H*60*60*1000-objTime.M*60*1000)/(1000));
	}else{
		objTime.flag=false;
		objTime.H=0;
		objTime.M=0;
		objTime.S=0;
	}
	objTime.H=filterNum(objTime.H);
	objTime.M=filterNum(objTime.M);
	objTime.S=filterNum(objTime.S);
	if(objTime.flag){
		$('.seckillTime .timeT').text('距离秒杀开始');
	}else{
		$('.seckillTime .timeT').text('距离秒杀结束');
	}
	$('.seckillTime .H').text(objTime.H);
	$('.seckillTime .M').text(objTime.M);
	$('.seckillTime .S').text(objTime.S);
}
function filterNum(num){
	return num>=10?num:'0'+num;
}
seckillInit();
$('.productL').on('click',function(){
	if(_Global_==='app'){
		action(20171104);
	}else{
		location.href='./carProduct.html';
	}

})
$('.productF').on('click',function(){
	if(_Global_==='app'){
		action(20171105);
	}else{
		location.href='./goldenFall.html';
	}
})
function goHistory(){
	var userLogins=_Global_=='web'?getCookie('userName'):phone;
	if(userLogins){
		location.href='./award_record.html';
	}else{
		if(_Global_=='web'){
			login=true;
			alertVerification();
		}else{
			signIn();
		}
	}

}




// ----------------------------------------------------------------- 以下是分享部分
//分享记录
function saveRecord(actId, type) {
var json = {
	'ACT_ID': actId,
	'SHARE_TYPE': type,
	'MOBILE': _Global_ == 'web' ? getCookie('userName') : phone
}
$.ajax({
	type: "post",
	async: false,
	dataType: "json",
	url: getIp + "/CARNT/Act/createShareRecord.action",
	data: json,
	success: function() {}
});
}
// 点击数
var json = {
'actId': 20171101
}

$.ajax({
type: "post",
async: false,
dataType: "json",
url: getIp + "/CARNT/Act/updateActClick.action",
data: json,
success: function(data) {
	//slert("ok");
}
})
var title;
var desc;
var imgUrl;
var linkUrl;
//分享文案
$.ajax({
type: "post",
async: false,
dataType: "json",
url: getIp + "/CARNT/ActWeb/getActivityById.action",
data: {
	ACT_ID: '20171101'
},
success: function(data) {
	title = data.RESULT_DATA[0].SHARE_TITLE;
	desc = data.RESULT_DATA[0].SHARE_CONTENT;
	imgUrl = data.RESULT_DATA[0].SHARE_ICON;
	linkUrl = data.RESULT_DATA[0].LINK_URL;
	document.title = title;
}
});
//获取分享的文案
var timestamp;
var nonceStr;
var signature;
//这个URL是你当前页面的url
var json = {
'url': location.href
}
var u = navigator.userAgent;
var isAndroids = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; // android终端
var isiOSs = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
var urls='';
var appid='';
if(isAndroids){
	urls='../../OAuth/getAccess_token.action';
	appid='wxf71262a112685c25';
}else if(isiOSs){
	urls='../../OAuth/getAccess_tokenIPHONE.action';
	appid='wx054c6f61cacfa911';
}
$.ajax({
type: "post",
async: false,
dataType: "json",
url: urls,
data: json,
success: function(data) {
	timestamp = data.NOW_TIME;
	nonceStr = data.NONCESTR;
	signature = data.SKEY;
	wx.config({
		debug: false, // 开启调试模式,false调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
		appId: appid, // 必填，公众号的唯一标识
		timestamp: timestamp, // 必填，生成签名的时间戳
		nonceStr: nonceStr, // 必填，生成签名的随机串
		signature: signature, // 必填，签名，见附录1
		jsApiList: [
			'onMenuShareTimeline',
			'onMenuShareAppMessage',
			'hideOptionMenu',
			'showOptionMenu'
		]
	});
}
});
wx.ready(function() {

// 分享给朋友
wx.onMenuShareAppMessage({
	title: title,
	desc: desc,
	link: linkUrl,
	imgUrl: imgUrl,
	trigger: function(res) {},
	success: function(res) {
		saveRecord(20171101, 1);
	},
	cancel: function(res) {},
	fail: function(res) {
		alert(JSON.stringify(res));
	}
});

//分享朋友圈
wx.onMenuShareTimeline({
	title: title,
	desc: desc,
	link: linkUrl,
	imgUrl: imgUrl,
	trigger: function(res) {

	},
	success: function(res) {
		saveRecord(20171101, 1);
	},
	cancel: function(res) {},
	fail: function(res) {
		alert(JSON.stringify(res));
	}
});
});
